
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.3 一对多 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="3.4多对多.html" />
    
    
    <link rel="prev" href="3.2包含.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.写在前面的话.html">
            
                <a href="1.写在前面的话.html">
            
                    
                    写在前面的话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="2.搭建环境.html">
            
                <a href="2.搭建环境.html">
            
                    
                    第一章 搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    第二章 模型定义
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.sequelize 实例变量.html">
            
                <a href="3.sequelize 实例变量.html">
            
                    
                    2.1 实例变量方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="4.定义一个模型.html">
            
                <a href="4.定义一个模型.html">
            
                    
                    2.2 定义一个模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="5.模型字段类型.html">
            
                <a href="5.模型字段类型.html">
            
                    
                    2.3 模型字段类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="6.导入与配置.html">
            
                <a href="6.导入与配置.html">
            
                    
                    2.3 导入与配置
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="3模型关系.html">
            
                <a href="3模型关系.html">
            
                    
                    第三章 模型关系
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="3.1属于.html">
            
                <a href="3.1属于.html">
            
                    
                    3.1 属于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="3.2包含.html">
            
                <a href="3.2包含.html">
            
                    
                    3.2 包含
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.3" data-path="3.3一对多.html">
            
                <a href="3.3一对多.html">
            
                    
                    3.3 一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="3.4多对多.html">
            
                <a href="3.4多对多.html">
            
                    
                    3.4 多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="3.5多态一对多.html">
            
                <a href="3.5多态一对多.html">
            
                    
                    3.5 多态一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="3.6多态多对多.html">
            
                <a href="3.6多态多对多.html">
            
                    
                    3.6 多态多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="3.7关系嵌套.html">
            
                <a href="3.7关系嵌套.html">
            
                    
                    3.7 关系嵌套
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="4模型查询.html">
            
                <a href="4模型查询.html">
            
                    
                    第四章 模型查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="5查询范围.html">
            
                <a href="5查询范围.html">
            
                    
                    第五章 查询范围 Scope (预定义查询条件)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="6生命周期函数.html">
            
                <a href="6生命周期函数.html">
            
                    
                    第六章 生命周期函数 Hook
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="7migrate.html">
            
                <a href="7migrate.html">
            
                    
                    第七章 数据库迁移 Migrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="8tabel2model.html">
            
                <a href="8tabel2model.html">
            
                    
                    第八章 根据 Table 生成 Model
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.3 一对多</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x4E00;&#x5BF9;&#x591A;">&#x4E00;&#x5BF9;&#x591A;</h1>
<p><strong>&#x4E00;&#x4E2A;&#x4EBA;&#x53EF;&#x4EE5;&#x6709;&#x5F88;&#x591A;&#x672C;&#x4E66;</strong>&#x5C31;&#x662F;<strong>&#x4E00;&#x5BF9;&#x591A;&#x5173;&#x7CFB;</strong>&#xFF0C;&#x7528;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x6765;&#x63CF;&#x8FF0;&#x5C31;&#x662F; <code>Person.hasMany(Book)</code>&#xFF0C;&#x6B64;&#x65F6;&#x7684;&#x4E3B;&#x4F53;&#x662F; Person &#x3002;&#x8FD9;&#x6837;&#x4F1A;&#x4E3A; Person &#x5B9E;&#x4F8B;&#x4E0A;&#x9762;&#x6DFB;&#x52A0;&#x4EE5;&#x4E0B;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-text">getBooks 
addBooks
addBook
createBook
removeBook
removeBooks
hasBook
hasBooks
countBooks
</code></pre>
<p>&#x800C;&#x5BF9;&#x4E8E; Book &#x6765;&#x8BF4;&#xFF0C;&#x5E94;&#x8BE5;&#x662F; Book &#x5C5E;&#x4E8E;&#x4E00;&#x4E2A;&#x4EBA;&#xFF0C;&#x800C;&#x4E0D;&#x662F; Book &#x6709;&#x4E00;&#x4E2A;&#x4EBA;&#xFF0C;&#x6240;&#x4EE5;&#x662F;&#x8981;&#x7528;&#x7684; blongsTo &#x65B9;&#x6CD5;&#xFF0C;&#x5373;<code>Book.belongsTo(Person)</code>&#xFF0C;&#x800C;&#x4F1A;&#x4E3A; Book &#x6DFB;&#x52A0;&#x4EC0;&#x4E48;&#x65B9;&#x6CD5;&#x5728;&#x4E0A;&#x4E00;&#x8282;&#x91CC;&#x9762;&#x6709;&#x63D0;&#x5230;&#x8FC7;&#xFF0C;&#x5927;&#x5BB6;&#x53EF;&#x4EE5;&#x81EA;&#x884C;&#x56DE;&#x987E;&#x4E0A;&#x8282;&#x5185;&#x5BB9;&#x3002;</p>
<blockquote>
<p>&#x5BF9;&#x4E8E;&#x4E00;&#x5BF9;&#x591A;&#x5173;&#x7CFB;&#xFF0C;&#x4F1A;&#x628A;&#x5C11;&#x7684;&#x4E00;&#x65B9;&#x7684; Id &#x653E;&#x5728;&#x591A;&#x7684;&#x4E00;&#x65B9;&#x7684;&#x8868;&#x4E0A;&#xFF0C;&#x5373;Book &#x8868;&#x6709;&#x4E00;&#x4E2A;userId &#x5B57;&#x6BB5;
&#x5047;&#x5982;&#x4F7F;&#x7528; Book.hasOne(person) &#x58F0;&#x660E;&#x6A21;&#x578B;&#x95F4;&#x7684;&#x5173;&#x7CFB;, &#x5219;&#x4F1A;&#x5728; Person &#x8868;&#x4E0A;&#x9762;&#x52A0;&#x4E00;&#x4E2A; userId &#x5B57;&#x6BB5;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4E00;&#x4E2A;&#x7528;&#x6237;&#x53EA;&#x80FD;&#x8BB0;&#x5F55;&#x4E00;&#x4E2A; id &#x503C;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x91CC;&#x7684; Book &#x662F;&#x591A;&#x7684;&#x5173;&#x7CFB;&#xFF0C;&#x5047;&#x5982;&#x7B2C;&#x4E8C;&#x672C;&#x4E66;&#x6765;&#x4E86;&#xFF0C;&#x600E;&#x4E48;&#x529E;&#xFF1F;&#x96BE;&#x9053;&#x590D;&#x5236;&#x4E00;&#x884C;&#x7528;&#x6237;&#xFF1F;&#x90A3;&#x8FD9;&#x6837;&#x5C82;&#x4E0D;&#x662F;&#x7528;&#x6237;&#x6709;&#x591A;&#x5C11;&#x672C;&#x4E66;&#xFF0C;User &#x8868;&#x91CC;&#x9762;&#x5C31;&#x6709;&#x591A;&#x5C11;&#x6761;&#x91CD;&#x590D;&#x7684;&#x884C;&#x3002;&#x54EA;&#x6015;&#x4F60;&#x662F;&#x571F;&#x8C6A;&#xFF0C;&#x4F60;&#x7684;&#x6570;&#x636E;&#x5E93;&#x7A7A;&#x95F4;&#x4E5F;&#x4E0D;&#x80FD;&#x8FD9;&#x6837;&#x6325;&#x970D;&#x5440;&#x3002;</p>
</blockquote>
<h3 id="&#x793A;&#x4F8B;">&#x793A;&#x4F8B;</h3>
<p>&#x7EE7;&#x7EED;&#x4F7F;&#x7528;&#x4E0A;&#x4E00;&#x8282;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x628A;&#x4E00;&#x5BF9;&#x4E00;&#x5173;&#x7CFB;&#x6539;&#x6210;&#x4E00;&#x5BF9;&#x591A;&#x5173;&#x7CFB;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x628A; hasOne &#x6539;&#x6210; hasMany &#x5373;&#x53EF;&#x3002;</p>
<h4 id="1--&#x4FEE;&#x6539;-userinstance-&#x63A5;&#x53E3;&#xFF0C;&#x6CE8;&#x91CA;&#x6389;-hasone-&#x5173;&#x7CFB;&#x7684;&#xFF0C;&#x5199;&#x4E0B;-hasmany-&#x7684;">1 . &#x4FEE;&#x6539; UserInstance &#x63A5;&#x53E3;&#xFF0C;&#x6CE8;&#x91CA;&#x6389; hasOne &#x5173;&#x7CFB;&#x7684;&#xFF0C;&#x5199;&#x4E0B; hasMany &#x7684;</h4>
<pre><code class="lang-ts"><span class="hljs-comment">//   Book: Sequelize.HasOneGetAssociationMixin&lt;BookInstance&gt;,</span>
<span class="hljs-comment">//   setBook: Sequelize.HasOneSetAssociationMixin&lt;BookAttributes, number&gt;,</span>
<span class="hljs-comment">//   createBook: Sequelize.HasOneCreateAssociationMixin&lt;BookAttributes&gt;,</span>

  getBooks: Sequelize.HasManyGetAssociationsMixin&lt;BookInstance&gt;,
  createBook: Sequelize.HasManyCreateAssociationMixin&lt;BookAttributes, BookInstance&gt;,
  addBook: Sequelize.HasManyAddAssociationMixin&lt;BookInstance, <span class="hljs-built_in">number</span>&gt;,
  addBooks: Sequelize.HasManyAddAssociationsMixin&lt;BookInstance, <span class="hljs-built_in">number</span>&gt;,
  hasBook: Sequelize.HasManyHasAssociationMixin&lt;BookInstance, <span class="hljs-built_in">number</span>&gt;,
  hasBooks: Sequelize.HasManyHasAssociationsMixin&lt;BookInstance, <span class="hljs-built_in">number</span>&gt;,
  removeBook: Sequelize.HasManyRemoveAssociationMixin&lt;BookInstance, <span class="hljs-built_in">number</span>&gt;,
  removeBooks: Sequelize.HasManyRemoveAssociationsMixin&lt;BookInstance, <span class="hljs-built_in">number</span>&gt;,
  countBooks: Sequelize.HasManyCountAssociationsMixin,
</code></pre>
<h4 id="2--&#x4FEE;&#x6539;&#x5173;&#x7CFB;&#x58F0;&#x660E;">2 . &#x4FEE;&#x6539;&#x5173;&#x7CFB;&#x58F0;&#x660E;</h4>
<pre><code class="lang-ts">(User as <span class="hljs-built_in">any</span>).associate = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"><span class="hljs-keyword">this</span>: <span class="hljs-keyword">typeof</span> User,models</span>)</span>{
   <span class="hljs-comment">// this.hasOne(models.Book);</span>
   <span class="hljs-keyword">this</span>.hasMany(models.Book);
}
</code></pre>
<p>&#x73B0;&#x5728;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E00;&#x5BF9;&#x591A;&#x5173;&#x7CFB;&#x4E86;&#x3002;</p>
<h4 id="3--&#x6765;&#x5230;-indexts">3 . &#x6765;&#x5230; index.ts</h4>
<p>&#x628A;&#x4E4B;&#x524D;&#x7684;&#x6570;&#x636E;&#x5E93;&#x8868;&#x5220;&#x9664;&#x6389;&#xFF0C;&#x901A;&#x8FC7; sync &#x518D;&#x6B21;&#x91CD;&#x5EFA;&#xFF0C;&#x4F1A;&#x770B;&#x5230; Book &#x8868;&#x6709;&#x4E00;&#x4E2A; userId &#x5B57;&#x6BB5;&#x3002;</p>
<p>&#x9996;&#x5148;&#x8FD0;&#x884C;&#x4E0B;&#x9762;&#x7684;&#x4EE3;&#x7801;&#x589E;&#x52A0;&#x6570;&#x636E;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">let</span> book = await Book.create({author:<span class="hljs-string">&apos;alice&apos;</span>, description: <span class="hljs-string">&apos;typescript hand book&apos;</span>, status: <span class="hljs-string">&apos;inSale&apos;</span>,title:<span class="hljs-string">&apos;ts leaning&apos;</span>});
<span class="hljs-keyword">let</span> user = await User.create({email:<span class="hljs-string">&apos;belovedyogurt@gmail.com&apos;</span>,name: <span class="hljs-string">&apos;yugo&apos;</span>})
book.setUser(user);
await book.save();
</code></pre>
<p>&#x4E4B;&#x540E;&#x5141;&#x8BB8;&#x4E0B;&#x9762;&#x7684;&#x62FF;&#x5230;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x4E14;&#x901A;&#x8FC7; getBooks &#x62FF;&#x5230;&#x8BE5;&#x7528;&#x6237;&#x62E5;&#x6709;&#x7684;&#x6240;&#x6709; books &#x5217;&#x8868;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> user = await User.findOne({
 where: {id: <span class="hljs-number">1</span>},
 include: [{ model: Book }]
});
<span class="hljs-keyword">const</span> books = await user.getBooks();
<span class="hljs-built_in">console</span>.log(books);
</code></pre>
<p>&#x8FD0;&#x884C;&#x4E4B;&#x540E;&#x53EF;&#x4EE5;&#x770B;&#x5230; <code>[ Book { .....</code> &#x8FD9;&#x5C31;&#x8BF4;&#x660E;&#x6211;&#x4EEC;&#x62FF;&#x5230;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#xFF0C;&#x6A21;&#x578B;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;&#x6B63;&#x786E;&#x8FDE;&#x63A5;&#x3002;</p>
<blockquote>
<p>&#x4E00;&#x5BF9;&#x4E00;&#x5173;&#x7CFB;&#x4F7F;&#x7528;&#xFF1A; belongsTo &#x3001;hasOne
&#x4E00;&#x5BF9;&#x591A;&#x5173;&#x7CFB;&#x4F7F;&#x7528;&#xFF1A;belongsTo&#x3001;hasMany</p>
</blockquote>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="3.2包含.html" class="navigation navigation-prev " aria-label="Previous page: 3.2 包含">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="3.4多对多.html" class="navigation navigation-next " aria-label="Next page: 3.4 多对多">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.3 一对多","level":"1.5.3","depth":2,"next":{"title":"3.4 多对多","level":"1.5.4","depth":2,"path":"document/3.4多对多.md","ref":"./document/3.4多对多.md","articles":[]},"previous":{"title":"3.2 包含","level":"1.5.2","depth":2,"path":"document/3.2包含.md","ref":"./document/3.2包含.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"document/3.3一对多.md","mtime":"2017-06-28T03:29:57.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-28T08:29:37.466Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

